package apisvr

import (
	"context"

	"gitee.com/linxing_3/sr-core/app"
	"gitee.com/linxing_3/sr-core/app/web"
	"gitee.com/linxing_3/sr-core/config"
	"gitee.com/linxing_3/sr-core/config/source"
	"gitee.com/mkwchecking/sr-admin/cmd/args"
	"gitee.com/mkwchecking/sr-admin/cmd/domain"
)

func newApiSvrApp(ctx context.Context, cfg *args.ConfigArgs) (app.IApp, error) {

	// 1. create app
	app, err := web.NewApp(ctx,
		source.WithCfgFile(cfg.Config),
		source.WithEnvPrefix("SR"),
		source.WithDidScan(func() {
			if cfg.StaticDist != "" {
				config.Get().HttpServer.Static = cfg.StaticDist
			}
		}))

	if err != nil {
		return nil, err
	}

	// 2. inject domains
	for _, f := range domain.GetDomainsInject() {
		m, err := f.Gen(app.Context())
		if err != nil {
			return nil, err
		}
		app.AddDomain(m)
	}

	return app, nil
}
